<?php

namespace Apps\AppApi\Method\User;


use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\UserBase;
use Apps\Common\Models\SysAreaZipDistrictPhonecode;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\UserFollow;
use Apps\Common\Libs\SmaUtil;

/**
 * 获取基本信息
 */
class GetBaseInfo extends BaseMethod
{
    public function run()
    {
        // 验证用户权限
        if (!$this->verifyUserAuth()) {
            return false;
        }
        $field = [
            'user_cover','user_nickname','user_gender',
            'user_birthday','user_realname','user_district',
            'user_money', 'user_money_virtual', 'bind_phone user_mobile',
            'persional_sign', 'user_id', 'user_expert', 'expert_follow_count'
        ];
        $info = UserBase::query()->columns($field)->where('user_id='.$this->getUserAuth()->userId)->execute()->getFirst()->toArray();

        // 获取城市信息
        $SysAreaZipDistrictPhonecode = new SysAreaZipDistrictPhonecode();
        $city = $SysAreaZipDistrictPhonecode->getOne($info['user_district']);
        $info['user_district'] = $city['FULL_NAME'] ?: '';

        $info['user_cover'] = $info['user_cover'] ? PicUrl::UserCover($info['user_cover'], $this->getDi()) : '';
        $info['persional_sign'] = $info['persional_sign'] ? unserialize(base64_decode($info['persional_sign'])) : '';

        // 加入关注数和粉丝数
        $UserFollow = new UserFollow();
        $info['follow_count'] = $UserFollow->getFollowCount($info['user_id'], 1);
        if ($info['user_expert']) {
            $info['fans_count'] = $info['expert_follow_count'];
        } else {
            $info['fans_count'] = $UserFollow->getToFollowCount($info['user_id'], 1);
        }
        unset($info['expert_follow_count']);

        //获取用户账号
        $userid = $this->getUserAuth()->userId;
        $mobile = UserBase::findFirst("user_id = {$userid}")->user_account;
        $smautil = new SmaUtil();
        $rs = $smautil->getIdentity($mobile);
        //加入身份
        if ($rs){
            switch ($rs['type']){
                case 1:
                case 2:
                    $info['identity'] = 3;
                    break;
                case 3:
                    $info['identity'] = 1;
                    break;
                case 4:
                    $info['identity'] = 2;
                    break;
                default:
                    $info['identity'] = 3;
                    break;
            }
        }
        else {
            $info['identity'] = 3;//普通用户
        }
        $this->setResult($info);
    }
}